home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / ENTRY.SWG / 0008_Masked Input.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  3KB  |  96 lines

  1. {
  2. >  The text on the screen would be something like:
  3. >  What is your phone number? (   )   -
  4. >                              ^^^ ^^^ ^^^^
  5. >  But text could only be entered at the marked locations.  As soon as one
  6. >  section is full it would move to the one beside it but read in a different
  7. >  variable..
  8.  
  9. How about this: (it's tested, BTW)
  10. }
  11.  
  12. USES Crt;
  13.  
  14. VAR
  15.   ts : String;
  16.  
  17. PROCEDURE MaskedReadLn(VAR s : String; mask : String; fillCh : Char);
  18. { in 'mask', chars with A will only accept alpha input, and chars
  19.   with 0 will only accept numeric input; spaces accept anything }
  20. VAR ch : Char; sx, ox, oy : Byte;
  21. BEGIN
  22.   s := ''; ox := WhereX; oy := WhereY; sx := 0;
  23.   REPEAT
  24.     Inc(sx);
  25.     IF (mask[sx] IN ['0', 'A']) THEN
  26.       Write(fillCh)
  27.     ELSE IF (mask[sx] = '_') THEN
  28.       Write(' ')
  29.     ELSE Write(mask[sx]);
  30.   UNTIL (sx = Length(mask));
  31.   sx := 0;
  32.   WHILE (NOT (mask[sx + 1] IN [#32, '0', 'A']))
  33.   AND (sx < Length(mask)) DO BEGIN
  34.     Inc(sx);
  35.     s := s + mask[sx];
  36.   END;
  37.   GotoXY(ox + sx, oy);
  38.   REPEAT
  39.     ch := ReadKey;
  40.     IF (ch = #8) THEN BEGIN
  41.       IF (Length(s) > sx) THEN BEGIN
  42.         IF NOT (mask[Length(s)] IN [#32, '0', 'A']) THEN BEGIN
  43.           REPEAT
  44.             s[0] := Chr(Length(s) - 1);
  45.             GotoXY(WhereX - 1, WhereY);
  46.           UNTIL (Length(s) <= sx) OR (mask[Length(s)] IN [#32, '0', 'A']);
  47.         END;
  48.         s[0] := Chr(Length(s) - 1); GotoXY(WhereX - 1, WhereY);
  49.         Write(fillCh); GotoXY(WhereX - 1, WhereY);
  50.       END ELSE BEGIN
  51.         Sound(440);
  52.         Delay(50);
  53.         NoSound;
  54.       END;
  55.     END ELSE IF (Length(s) < Length(mask)) THEN BEGIN
  56.       CASE mask[Length(s) + 1] OF
  57.         '0' : IF (ch IN ['0'..'9']) THEN BEGIN
  58.                 Write(ch);
  59.                 s := s + ch;
  60.               END;
  61.         'A' : IF (UpCase(ch) IN ['A'..'Z']) THEN BEGIN
  62.                 Write(ch);
  63.                 s := s + ch;
  64.               END;
  65.         #32 : BEGIN
  66.                 Write(ch);
  67.                 s := s + ch;
  68.               END;
  69.       END;
  70.       WHILE (Length(s) < Length(mask))
  71.       AND (NOT (mask[Length(s) + 1] IN [#32, '0', 'A'])) DO BEGIN
  72.         IF (mask[Length(s) + 1] = '_') THEN s := s + ' ' ELSE
  73.           s := s + mask[Length(s) + 1];
  74.         GotoXY(WhereX + 1, WhereY);
  75.       END;
  76.     END;
  77.   UNTIL (ch IN [#13, #27]);
  78. END;
  79.  
  80. BEGIN
  81.   ClrScr;
  82.   Write('Enter phone number: ');
  83.   MaskedReadLn(ts, '(000)_000-0000', '_');
  84.   WriteLn;
  85.   Write('Enter postal code: ');
  86.   MaskedReadLn(ts, 'A0A_0A0', '_');
  87.   WriteLn;
  88. END.
  89.  
  90. {
  91. It can be improved with colours and such stuff, but it may suit your
  92. needs without enhancement.  If you have questions about how this works,
  93. feel free to ask.
  94. }
  95.  
  96.